package com.ldl.exam.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.ldl.exam.constant.Constants;
import com.ldl.exam.entity.*;
import com.ldl.exam.exception.ExamException;
import com.ldl.exam.service.ExamPaperService;
import com.ldl.exam.util.AjaxResult;
import com.ldl.exam.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author liudaolin
 * @version 1.0
 * @date 2022/10/02 15:22:11
 */
@RestController
@RequestMapping("/front/paper")
public class ExamPaperController {
    @Autowired
    private ExamPaperService examPaperService;

    // 根据 stuId 分页查询 学生的所有考试的基本信息 已测试
    @RequestMapping("/findExams")
    public AjaxResult findExamsByStuIdAndPage(Integer pageNum, Integer pageSize, HttpSession session) {
        // 1.只能预览自己的考试基本信息
        Stu stuLogin = (Stu) session.getAttribute(Constants.STU_LOGIN_KEY);
        if(null == pageSize) {
            pageSize = Constants.DEFAULT_PAGE_SIZE;
        }
        // 2.未统计排名的试卷也不能查看: score_rank = -1
        PageInfo<StuExamPaper> pageInfo = examPaperService.findExamsByStuIdAndPage(stuLogin.getId(), pageNum, pageSize);
        return AjaxResult.isOk("查询成功", pageInfo);
    }

    // 预览学生的某个试卷内容 已测试
    @RequestMapping("/previewExam")
    public AjaxResult previewExamByStuIdAndExamId(Integer examPaperId, HttpSession session) {
        // 1.只能预览自己的试卷
        Stu stuLogin = (Stu) session.getAttribute(Constants.STU_LOGIN_KEY);

        // 2.未统计排名的试卷也不能查看: score_rank = -1
        StuExamPaper stuExamPaper = examPaperService.previewExamByStuIdAndExamId(stuLogin.getId(), examPaperId);
        Integer scoreRank = stuExamPaper.getScoreRank();
        if(scoreRank == null || scoreRank == -1 ) {
            throw new ExamException("试卷还未批改完毕");
        }
        return AjaxResult.isOk("查询成功", stuExamPaper);
    }

    // 验证考试码是否正确 已测试
    @RequestMapping("/checkExamCode")
    public AjaxResult checkExamCode(String code) {
        if(CommonUtil.isEmpty(code)) {
            throw new ExamException("验证码不能为空");
        }

        examPaperService.checkExamCode(code);
        return AjaxResult.isOk("验证码正确");
    }


    // 某学生开始考试, 返回其试卷内容 已测试
    @RequestMapping("/startExam")
    public AjaxResult startExam(String code) {
        // 1.必须是登录的学生, 才能参与考试
        if(CommonUtil.isEmpty(code)) {
            throw new ExamException("验证码不能为空");
        }

        ExamPaper examPaper = examPaperService.findExam(code);
        return AjaxResult.isOk("试卷加载成功", examPaper);
    }

    // 交卷 已测试
    @RequestMapping("/submit")
    public AjaxResult submitExam(Integer examPaperId, Integer count, String content, HttpSession session) {
        Stu login = (Stu) session.getAttribute(Constants.STU_LOGIN_KEY);

        ObjectMapper mapper = new ObjectMapper();
        TypeReference<List<StuExamAnswer>> typeReference = new TypeReference<List<StuExamAnswer>>() {};
        try {
            List<StuExamAnswer> answers = mapper.readValue(content, typeReference);

            Integer stuId = login.getId();
            examPaperService.addStuAnswers(examPaperId, stuId, count, answers);
            return AjaxResult.isOk("成功交卷");
        } catch (JsonProcessingException e) {
            return AjaxResult.isFail("试卷提交失败,原因: "+ e.getMessage() +" ,请联系管理员.");
        }
    }

}
